package com.xiaoming.project.core.thread;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class DaemonThreadTest {

    public static void main(String[] args) {
        MyDaemonThread.appendPrintMessage("小明");
        MyDaemonThread.appendPrintMessage("haha");
    }

    static class MyDaemonThread implements Runnable {

        private static BlockingDeque<String> blockingDeque = new LinkedBlockingDeque();

        private MyDaemonThread() {
        }

        static {
            Thread thread = new Thread(new MyDaemonThread());
            thread.setDaemon(true);
            thread.start();
        }

        public static void appendPrintMessage(String message) {
            blockingDeque.add(message);
        }

        public synchronized void execute() {
            try {
                System.out.println(blockingDeque.take());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void run() {
            while (true) {
                execute();
            }
        }

    }

}